package com.fengxing.ams.pub.util;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import com.fengxing.ams.pub.AMSException;

public class ConvertUtil {
    /**
     * Convert string to int, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static int toInt(String value, int defaultValue) {
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        try {
            return Integer.parseInt(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert string to Integer, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static Integer toInteger(String value, Integer defaultValue) {
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        try {
            return Integer.valueOf(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert string to long, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static long toLong(String value, long defaultValue) {
		if (StringUtils.isBlank(value)) {
			return defaultValue;
		}
        try {
            return Long.parseLong(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert string to long, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static Long toLong(String value, Long defaultValue) {
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        try {
            return Long.valueOf(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert string to float, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static float toFloat(String value, float defaultValue) {
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        
        try {
            return Float.parseFloat(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert string to Float, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static Float toFloat(String value, Float defaultValue) {
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        
        try {
            return Float.valueOf(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert string to double, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static double toDouble(String value, double defaultValue) {
		String newValue = (null == value ? null : (value.indexOf('$') != -1 ? value.replaceAll("[\\$,]", "") : value));
		if (StringUtils.isBlank(newValue)) {
			return defaultValue;
		}
		try {
			return Double.parseDouble(newValue);
		} catch (NumberFormatException e) {
			return defaultValue;
		}
    }
    
    /**
     * Convert string to double, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static Double toDouble(String value, Double defaultValue) {
        String newValue = (null == value ? null : (value.indexOf('$') != -1 ? value.replaceAll("[\\$,]", "") : value));
        if (StringUtils.isBlank(newValue)) {
            return defaultValue;
        }
        try {
            return Double.valueOf(newValue);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert string to boolean, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static boolean toBoolean(String value, boolean defaultValue) {
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        if ("on".equals(value)) {
            return true;
        }
        if ("off".equals(value)) {
            return false;
        }
        try {
            return Boolean.parseBoolean(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert string to boolean, if the string is invalid, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static Boolean toBoolean(String value, Boolean defaultValue) {
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        if ("on".equals(value)) {
            return Boolean.TRUE;
        }
        if ("off".equals(value)) {
            return Boolean.FALSE;
        }
        try {
            return Boolean.valueOf(value);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
    
    /**
     * Convert object to string, if the string is empty, then return the default value.
     * @param value
     * @param defaultValue
     * @return
     */
    public static String toString(Object value, String defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        if (StringUtils.isBlank(value.toString())) {
            return defaultValue;
        }

        return value.toString();
    }
    
    /**
     * Convert Date to format String
     * @param value
     * @return
     */
    public static String toString(Date value) {
		if (value == null){
			return "";
		}
		return SimpleDateFormatThreadLocal.get().format(value);
	}
    
    /**
     * Convert Date to fromat String by format
     * @param value
     * @param format
     * @return
     */
    public static String toString(Date value, String format) {
        if (value == null){
            return "";
        }
		if (StringUtils.isBlank(format)) {
			return toString(value);
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(value);
	}
    
    /**
     * Convert string to a date using default format
     * @param value
     * @return
     */
    public static Date toDate(String value) {
        if (value.indexOf(":") == -1) {
            value += " 00:00:00";
        }
        try {
            return SimpleDateFormatThreadLocal.get().parse(value);
        } catch (ParseException e) {
            throw new AMSException("Date(" + value + ") is invalid");
        }
    }
    
    /**
     * Convert string to a date using given format
     * @param value
     * @param format
     * @return
     */
    public static Date toDate(String value, String format) {
        if (StringUtils.isBlank(format)) {
            return toDate(value);
        }
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.parse(value);
        } catch (ParseException e) {
            throw new AMSException("Date(" + value + ") is invalid");
        }
    }
    
    /**
     * Convert string to a date using given format, if the value is invalid, then return default date.
     * @param value
     * @param format
     * @param defaultValue
     * @return
     */
    public static Date toDate(String value, String format, Date defaultValue) {
        if (StringUtils.isBlank(value)) {
            return defaultValue;
        }
        try {
            return StringUtils.isBlank(format) ? toDate(value) : toDate(value, format);
        } catch (AMSException e) {
            return defaultValue;
        }
    }
    
	/**
	 * format double value to maxinum fraction digits, if the value is null, then return null
	 * 
	 * @param value
	 * @param maximumFractionDigits
	 * @return
	 */
	public static Double formatDecimal(Double value, int maximumFractionDigits) {
        if (value == null) {
            return null;
        }
		BigDecimal b = new BigDecimal(value);
		return b.setScale(maximumFractionDigits, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
    
	public static double toTwoDecimal(Double value) {
		return formatDecimal(value, 2);
	}
}
